package com.boc.langxiang.ui.activity.adm;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.boc.langxiang.R;
import com.boc.langxiang.contract.adm.AdmSimpleContract;
import com.boc.langxiang.entity.events.AdmFollowEvent;
import com.boc.langxiang.entity.response.AdmDetailEntity;
import com.boc.langxiang.entity.response.AdmFollowContent;
import com.boc.langxiang.entity.response.PhotoEntity;
import com.boc.langxiang.manager.PicassoImageLoader;
import com.boc.langxiang.presenter.adm.AdmSimplePresenter;
import com.boc.langxiang.ui.activity.BaseActivity;
import com.boc.langxiang.ui.view.SongTextView;
import com.boc.langxiang.ui.view.roundedimageview.RoundedImageView;
import com.boc.langxiang.util.StringUtil;
import com.boc.langxiang.util.T;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AdmSimpleActivity extends BaseActivity implements AdmSimpleContract.View {

    @BindView(R.id.head)
    RoundedImageView mHead;
    @BindView(R.id.name)
    SongTextView mName;
    @BindView(R.id.job)
    SongTextView mJob;
    @BindView(R.id.art_count)
    SongTextView mArtCount;
    @BindView(R.id.hot_count)
    SongTextView mHotCount;
    @BindView(R.id.star)
    ImageView mStar;
    @BindView(R.id.iamge1)
    ImageView mIamge1;
    @BindView(R.id.iamge2)
    ImageView mIamge2;
    @BindView(R.id.iamge3)
    ImageView mIamge3;
    @BindView(R.id.look_detail)
    TextView mLookDetail;
    @BindView(R.id.close)
    ImageView mClose;
    private AdmSimpleContract.Presenter mPresenter;
    private String mDesignerId;
    private AdmDetailEntity mAdmDetail;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_adm_simple);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);

        Intent intent = getIntent();
        mDesignerId = intent.getStringExtra("designerId");
        if (mDesignerId == null) {
            finish();
            return;
        }

        mPresenter = new AdmSimplePresenter(this);
        mPresenter.admDetail(mDesignerId);
    }

    @OnClick({R.id.star, R.id.look_detail, R.id.close})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.star:
                if (isLogin()) {
                    mPresenter.admFollow(mDesignerId);
                }
                break;
            case R.id.look_detail:
                if (mAdmDetail == null) {
                    return;
                }
//                if (isLogin()) {
                    Intent intent = new Intent(mContext, ArtistIndexActivity.class);
                    intent.putExtra("designerId", mAdmDetail.getId());
                    startActivity(intent);
//                }
                break;
            case R.id.close:
                finish();
                break;
        }
    }

    @Override
    public void admDetailSuccess(AdmDetailEntity admDetail) {
        mAdmDetail = admDetail;
        PhotoEntity photo = admDetail.getPhoto();
        if (photo != null) {
            PicassoImageLoader.getInstance().displayUrlImage(mContext, photo.getUrl(), mHead);
        } else {
            PicassoImageLoader.getInstance().displayUrlImage(mContext, null, mHead);
        }

        mName.setText(StringUtil.getValue(admDetail.getNickname()));
        mJob.setText(StringUtil.getValue(admDetail.getZhiwu()));
        mArtCount.setText("作品  " + StringUtil.getValue(admDetail.getProduct_num() + ""));
        mHotCount.setText("热度  " + StringUtil.getValue(admDetail.getClick_num() + ""));

        int click_type = admDetail.getClick_type();
//        matchStar(click_type);
        switch (click_type) {
            case AdmFollowContent.CANCEL:
                mStar.setImageResource(R.drawable.icon_star_white_kong);
                break;
            case AdmFollowContent.SUCCESS:
                mStar.setImageResource(R.drawable.icon_star_white_solid);
                break;
            case AdmFollowContent.FAIL:
                break;
        }

        List<String> bannerurl = admDetail.getBannerurl();
        if (bannerurl != null) {
            for (int i = 0; i < bannerurl.size(); i++) {
                String s = bannerurl.get(i);
                if (i == 0) {
                    PicassoImageLoader.getInstance().displayUrlImage(mContext, s, mIamge1);
                } else if (i == 1) {
//                    ImageLoader.getInstance().displayUrlImage(s, mIamge2);
                    PicassoImageLoader.getInstance().displayUrlImage(mContext, s, mIamge2);
                } else if (i == 2) {
//                    ImageLoader.getInstance().displayUrlImage(s, mIamge3);
                    PicassoImageLoader.getInstance().displayUrlImage(mContext, s, mIamge3);
                }
            }
        }

    }

    private void matchStar(int start) {
        switch (start) {
            case AdmFollowContent.CANCEL:
                mStar.setImageResource(R.drawable.icon_star_white_kong);
                Toast.makeText(mContext, "取消关注", Toast.LENGTH_SHORT).show();
                break;
            case AdmFollowContent.SUCCESS:
                mStar.setImageResource(R.drawable.icon_star_white_solid);
                Toast.makeText(mContext, "关注成功", Toast.LENGTH_SHORT).show();
                break;
            case AdmFollowContent.FAIL:
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onAdmFollowEvent(AdmFollowEvent event) {
        matchStar(event.type);
        mAdmDetail.setClick_type(event.type);
    }

    @Override
    public void showLoading() {
        showProgressDialog();
    }

    @Override
    public void hideLoading() {
        hideDialog();
    }

    @Override
    public void onError(String code, String msg) {
        T.showShort(mContext, msg);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
